//
//  PeiLastXVC.m
//  JinChengYun
//
//  Created by 金橙云 on 2017/4/21.
//  Copyright © 2017年 金橙云. All rights reserved.
//

#import "PeiLastXVC.h"

#import "TodoHttpManager.h"
#import "OneCustomCell.h"
#import "TwoCustomCell.h"
#import "CCZTableButton.h"
#import "NeiLiuView.h"
@interface PeiLastXVC ()<UITableViewDelegate,UITableViewDataSource,NeiLiuViewDelegate>
{
    NSMutableArray *_oneTitleArr;
    NSMutableArray *_btnTitleArr;
    NSString *aStr;
    NSString *bStr;
    NSString *cStr;
    NSString *dStr;
    NSString *eStr;
    NSString *fStr;
    NSString *gStr;
    NSString *hStr;
    NSString *iStr;
    NSString *jStr;
  
    
}

@property (nonatomic, strong) NeiLiuView *alertView;
@property(nonatomic,strong)UITableView *tabView;
@property(nonatomic,strong)NSMutableDictionary *houseZDic;//总的数据
@property(nonatomic,strong)NSMutableDictionary *jieZDic;
@property(nonatomic,strong)NSMutableDictionary *quaneZDic;
@property(nonatomic,strong)NSMutableArray *everyArr;
@property(nonatomic,strong)NSMutableArray *lastArr;
@property (nonatomic, strong) CCZTableButton *table2;
@end

@implementation PeiLastXVC

-(void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDZhuSYouHui:) name:TDZhuSYouHui object:nil];
    //TODO:房源优惠
    //  NSLog(@"======== houseID == %@",_houseID);
    [_manager userZhuShouYouHuiWithID:_IDStr withType:@"3" withStates:nil];
    
    //房源预留
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didTDZhuSYuLiu:) name:TDZhuSYuLiu object:nil];
}

-(void)didTDZhuSYouHui:(NSNotification *)notif
{
    if (notif.object != 0)
    {
        int status = [[notif.object objectForKey:@"status"] intValue];
        if (status == 1)
        {
            _houseZDic = [notif.object objectForKey:@"data"];
            //NSLog(@"总的字典内容----houseDic==%@",_houseZDic);
            
            aStr = [_houseZDic objectForKey:@"name"];
            bStr = [_houseZDic objectForKey:@"sale_price_all"];
          
          
            
            _everyArr = [_houseZDic objectForKey:@"data"];
            
            _quaneZDic = [_everyArr objectAtIndex:0];//按揭
            eStr = [_quaneZDic objectForKey:@"pay_way"];
            fStr = [_quaneZDic objectForKey:@"cash_discount"];
            gStr = [_quaneZDic objectForKey:@"discount"];
            hStr = [_quaneZDic objectForKey:@"xieyi_price_all"];
        
            _lastArr = [NSMutableArray arrayWithObjects:aStr,bStr,eStr,fStr,gStr,hStr, nil];
            //  NSLog(@"lastarr 按揭支付 =========%@",_lastArr);
            
            
            
            [_tabView reloadData];
        }
    }
}

//房源预留

-(void)didTDZhuSYuLiu:(NSNotification *)notifi
{
    if (notifi.object != 0)
    {
        int statue = [[notifi.object objectForKey:@"status"] intValue];
        if (statue == 1)
        {
            [_manager MakeMyselfAlert:@"成功" andIsNeedTitle:NO andShowTime:2.0];
            
            //            DetailsViewController *detailsVc = [[DetailsViewController alloc] init];
            //            [self.navigationController pushViewController:detailsVc animated:NO];
            [_tabView reloadData];
        }
        else
        {
            [_manager MakeMyselfAlert:@"失败" andIsNeedTitle:NO andShowTime:2.0];
        }
        
    }
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDZhuSYouHui object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:TDZhuSYuLiu object:nil];

    
}


- (void)viewDidLoad {
    [super viewDidLoad];
  self.title = @"房源管理";
    
    _manager = [TodoHttpManager sharedManager];
    
    _jieZDic = [NSMutableDictionary dictionary];
    _quaneZDic = [NSMutableDictionary dictionary];
    _houseZDic = [[NSMutableDictionary alloc] init];
    _lastArr = [NSMutableArray array];
    
    NSString *IDA = [[NSUserDefaults standardUserDefaults] objectForKey:@"STATE"];
    if ([IDA isEqualToString:@"2"])
    {
        UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(peiTaoLiuBtnClick)];
        
        self.navigationItem.rightBarButtonItem = rightBarItem;
        
        [self.navigationItem.rightBarButtonItem setTitle:@"内留"];
        [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
        
        
    }

    _oneTitleArr = [NSMutableArray arrayWithObjects:@"房源位置 :",@"销售总价 :",@"付款方式 :",@"总价优惠 :",@"折扣优惠 :",@"协议总价 :", nil];
    _btnTitleArr = [NSMutableArray arrayWithObjects:@"按揭支付",@"全款支付", nil];
    _tabView =[[UITableView alloc] initWithFrame:CGRectMake(0, 0, B_WIDTH, B_HEIGHT) style:UITableViewStylePlain];
    _tabView.delegate = self;
    _tabView.dataSource = self;
    _tabView.rowHeight = 40;
    _tabView.backgroundColor = BgColor;
    _tabView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:_tabView];

}
- (NeiLiuView *)alertView {
    if (!_alertView) {
        self.alertView = [[NeiLiuView alloc] initWithFrame:CGRectMake(30, 200, [UIScreen mainScreen].bounds.size.width - 60, 220)];
        self.alertView.backgroundColor = [UIColor whiteColor];
        self.alertView.delegate = self;
    }
    return _alertView;
}
-(void)peiTaoLiuBtnClick
{
    [self.alertView showView];//弹出
}


- (void)requestEventAction:(UIButton *)button {
    
    [self.alertView closeView];
    //保存 发送到服务器上面
    NSLog(@"回馈信息是 === %@",self.alertView.msgField.text);
    NSLog(@"跟进时间 === %@",self.alertView.oneLab.text);
    NSLog(@"下次跟进 === %@",self.alertView.xiaLab.text);
    
    [_manager userZhuShouYuLiuWithID:_IDStr withType:@"3" withLiuName:self.alertView.msgField.text withLiuTime:self.alertView.oneLab.text withBeiZhu:self.alertView.xiaLab.text];
 
    [self.tabView reloadData];
    
}


-(NSMutableArray *)everyArr
{
    if (_everyArr)
    {
        return  _everyArr;
    }
    _everyArr = [[NSMutableArray alloc] init];
    return _everyArr;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _oneTitleArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (indexPath.row == 2)
    {
        NSString *cellIndent = @"CellA";
        TwoCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndent];
        if (cell ==nil)
        {
            cell = [[TwoCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndent];
        }
        
        cell.aneTLab.text= @"付款方式:";
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];//取消点击
        
        CGRect rectInTableView = [tableView rectForRowAtIndexPath:indexPath];
        CGRect rect = [tableView convertRect:rectInTableView toView:[tableView superview]];
        cell.oneBtn.frame = rect;
        
        UIButton*deviceImageButton = cell.oneBtn;
        [deviceImageButton addTarget:self action:@selector(deviceButtonPressedClick:)
                    forControlEvents:UIControlEventTouchUpInside];
        self.table2 = [[CCZTableButton alloc] initWithFrame:CGRectMake(CGRectGetMinX(rect)+100, CGRectGetMaxY(rect), 260, 0)];
        [self.table2 addItems:_btnTitleArr exceptItem:@"全部"];
        [self.table2 selectedAtIndexHandle:^(NSUInteger index, NSString *itemName) {
            
            [cell.oneBtn setTitle:itemName forState:UIControlStateNormal];
            
            
            
            
            if (index == 1)
            {
                
                 [cell.oneBtn setTitle:@"全款支付" forState:UIControlStateNormal];
                _quaneZDic = [_everyArr objectAtIndex:1];//按揭
                eStr = [_quaneZDic objectForKey:@"pay_way"];
                fStr = [_quaneZDic objectForKey:@"cash_discount"];
                gStr = [_quaneZDic objectForKey:@"discount"];
                hStr = [_quaneZDic objectForKey:@"xieyi_price_all"];
                _lastArr = [NSMutableArray arrayWithObjects:aStr,bStr,eStr,fStr,gStr,hStr, nil];
                NSLog(@"lastarr 全款支付=========%@",_lastArr);
                [self.tabView reloadData];
               
            }
            else
            {
                 [cell.oneBtn setTitle:@"按揭支付" forState:UIControlStateNormal];
                _quaneZDic = [_everyArr objectAtIndex:0];//按揭
                eStr = [_quaneZDic objectForKey:@"pay_way"];
                fStr = [_quaneZDic objectForKey:@"cash_discount"];
                gStr = [_quaneZDic objectForKey:@"discount"];
                hStr = [_quaneZDic objectForKey:@"xieyi_price_all"];
                
                
                 _lastArr = [NSMutableArray arrayWithObjects:aStr,bStr,eStr,fStr,gStr,hStr, nil];
                NSLog(@"lastarr 按揭支付 =========%@",_lastArr);
                [self.tabView reloadData];
            }
            
            
            
            
            
        }];
        return cell;
        
    }
    else
    {
       NSString * cellIndent = @"CellB";
        OneCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndent];
        if (cell ==nil)
        {
            cell = [[OneCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndent];
            
            
        }
        if ([cell isKindOfClass:[OneCustomCell class]])
        {
            if (_houseZDic.count != 0)
            {
                
                NSString *str0 = (NSString *)_lastArr[indexPath.row];
                cell.twoLab.text = [NSString stringWithFormat:@"%@",str0];
               
          
            }
            
        }
        
        cell.oneLab.text = _oneTitleArr[indexPath.row];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];//取消点击
        
        
        return cell;
        
    }
    
    
    
    
    
    //return cell;
}

-(void)deviceButtonPressedClick:(id)sender
{
    
    UIView *v = [sender superview];//获取父类view
    TwoCustomCell *cell = (TwoCustomCell *)[v superview];//获取cell
    NSIndexPath *indexpath = [self.tabView indexPathForCell:cell];//获取cell对应的indexpath;
    NSLog(@"%@",indexpath);
    
    [self.table2 show];
    
    //  NSLog(@"设备图片按钮被点击:%ld%ld",(long)indexpath.section,(long)indexpath.row);
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
